<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	    
class Data_gudang extends Member_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->library(array('table','form_validation','server_processing'));
		$this->load->helper(array('form','url'));
		$this->load->model('data_gudang_model','',TRUE);
	}
	public function index()
	{
		$partial['_header']='template/header';
		$partial['_sidebar']="template/sidebar";
		$partial['_contents']="data_gudang/data_gudang";	
		$data['title']='Data Gudang';
		//set notiication
		$notif = $this->uri->segment(3);
		if($notif == '1')
		$data['notif'] = 'Data berhasil ditambah';
		else if($notif == '2')
		$data['notif'] = 'Data berhasil dihapus';
		else if($notif == '3')
		$data['notif'] = 'Update data success';
		else
		$data['notif'] = '';
		$this->template->load('index',$partial,$data);
	}
	
	function cek_id(){
		$id = $this->input->post('id_bahan');
		if(!empty($id)){
			$check=$this->data_gudang_model->cek_id($id);
			if($check->num_rows == 0)
				echo "true";
			else
				echo "false";
		}
		else
			echo "false";
	}
	function cek_name(){
		$name = $this->input->post('nama_bahan');
		if(!empty($name)){
			$check=$this->data_gudang_model->cek_name($name);
			if($check->num_rows == 0)
				echo "true";
			else
				echo "false";
		}
		else
			echo "false";
	}
	function cek_not_name(){
		$name = $this->input->post('nama_bahan');
		if(!empty($name)){
			$check=$this->data_gudang_model->cek_name($name);
			if($check->num_rows == 0)
				echo "false";
			else
				echo "true";
		}
		else
			echo "true";
	}
	function add(){
	$partial['_header']='template/header';
	$partial['_sidebar']="template/sidebar";
	$partial['_contents']="data_gudang/form_data_gudang";
		
	// set common properties
	$data['title'] = 'Data Gudang';
	$data['action'] = site_url('data_gudang/add/do');
	$data['form_attr'] = array('class' => 'form-horizontal', 'id' => 'FormGudang');
	$data['link_back'] = anchor('data_gudang','Back to list of data gudang',array('class'=>'back'));
	$data['disable'] = '';
	$model_name = array('satuan_model','kategori_bahan_model');
	$this->load->model($model_name,'',TRUE);
	$data['satuan']=$this->satuan_model->getAll()->result();
	$data['kategori_bahan']=$this->kategori_bahan_model->getAll()->result();
	
		if($this->uri->segment(3) === false){
		$data['message']='';
		$data['gudang']['id_bahan']='';
		$data['gudang']['nama_bahan']='';
		$data['gudang']['harga']='';
		$data['gudang']['stok']='';
		$data['gudang']['id_satuan']='';
		$data['gudang']['id_kategori_bahan']='';
		$data['link_back']=anchor('data_gudang','Cancel',array('class'=>'back'));
		$this->template->load('index',$partial,$data);
		}
		else{
		//save data
		$bahan=array('id_bahan'=>strtoupper($this->input->post('id_bahan')),
		'nama_bahan'=>$this->input->post('nama_bahan'),
		'stok'=>$this->input->post('jumlah'),
		'harga'=>$this->input->post('harga'),
		'id_satuan'=>$this->input->post('id_satuan'),
		'id_kategori_bahan'=>$this->input->post('id_kategori_bahan'));
		$this->data_gudang_model->save($bahan);
		//redirect to data list page
		redirect('data_gudang/index/1');		
		}
	}	

	function update($id){
	
	//set common properties
	$partial['_header']='template/header';
	$partial['_sidebar']="template/sidebar";
	$partial['_contents']="data_gudang/form_data_gudang";
	
	$data['title'] = 'Data Gudang';
	$data['action'] = site_url('data_gudang/update/'.$id.'/do');
	$data['form_attr'] = array('class' => 'form-horizontal', 'id' => 'FormGudang');
	$data['disable'] = 'disabled';		
	
	if(	$this->uri->segment(4) === false){
		$data['message']='';
		$data['gudang']=$this->data_gudang_model->get_by_id($id)->row_array();
		
		$this->load->model('satuan_model','',TRUE);
		$data['satuan']=$this->satuan_model->not($data['gudang']['id_satuan'])->result();
		$this->load->model('kategori_bahan_model','',TRUE);
		$data['kategori_bahan']=$this->kategori_bahan_model->not($data['gudang']['id_kategori_bahan'])->result();		
		$data['link_back']=anchor('data_gudang','Cancel',array('class'=>'back'));
		//load view
		$this->template->load('index',$partial,$data);
	}
	else{
		//save data
		$id=$this->input->post('id');
		$bahan=array('nama_bahan'=>$this->input->post('nama_bahan'),
		'stok'=>$this->input->post('jumlah'),
		'harga'=>$this->input->post('harga'),
		'id_satuan'=>$this->input->post('id_satuan'),
		'id_kategori_bahan'=>$this->input->post('id_kategori_bahan'));
	
		$this->data_gudang_model->update($id,$bahan);
		//redirect to data list page
		redirect('data_gudang/index/3');			
		}
	}
	
	function delete($id){
	//delete data
	$this->data_gudang_model->delete($id);
	//redirect to data list page
	redirect('data_gudang/index/2');
	}
	
	function _set_rules(){
	$this->form_validation->set_rules('id_bahan','Id','required');
	}
	
	function server(){
	/* ------inisialisasi tabel------- */
	//jika tidak di inisialisasi isi "", kecuali nama table wajib di isi
	//jika menggunakan join, nama field harus lengkap dengan tabelnya kecuali nama fieldnya uniq
	$field = array( 'id_bahan', 'nama_bahan', 'stok', 'harga', 'satuan.satuan', 'kategori_bahan.nama_kategori_bahan' );
	$table_name = "bahan_gudang";
	$uri = $this->uri->segment(1);
	$join = array("satuan","kategori_bahan");
	$on = array("bahan_gudang.id_satuan=satuan.id_satuan","kategori_bahan.id_kategori_bahan=bahan_gudang.id_kategori_bahan");
	$init = array('select' => $field, 'table_name' => $table_name,'join' => $join, 'on' => $on, 'uri' => $uri);
	$this->server_processing->server_data($_GET,$init);	
	}
}